
<template>
  <header>
    <div class="header-content">
      <div class="logo" @click="goToHome">
        <img src="https://www.usian.cn/uploads/20250327/052111edb1cc05f9fd4ecebfe45264ec.png" alt="码镜空间logo">
        <span>码镜空间</span>
      </div>
      <nav class="nav-links">
        <a
            v-for="category in navCategories"
            :key="category.id"
            :class="{ active: currentCategory === category.id }"
            @click="handleNavClick(category)"
            href="#">
          {{ category.name }}
        </a>
      </nav>
      <div class="search-container">
        <input type="text" placeholder="请输入关键字...">
        <button><i class="fa fa-search"></i></button>
      </div>
      <div class="user-action">
        <div class="action-links">
          <button class="icon-btn" @click="goToCart">
            <i class="fas fa-shopping-cart"></i>
            <span>购物车</span>
          </button>
          <button class="icon-btn" @click="goToLearningCenter">
            <i class="fas fa-graduation-cap"></i>
            <span>学习中心</span>
          </button>
        </div>
        <div class="auth-buttons">
          <button class="login-btn" @click="goToLogin">登录</button>
          <button class="register-btn" @click="goToRegister">注册</button>
        </div>
      </div>
    </div>
  </header>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const currentCategory = ref(null)

// 静态导航数据
const navCategories = ref([

])


import axios from "axios";


const fetchNavCategory = () => {
  axios.get("http://127.0.0.1:8000/api/nav/categoryis/").then(res => {
    console.log(res)
    if (res.data.code == 1) {
      navCategories.value = res.data.data.map(item => ({
        id: item.id,
        name: item.display_name,
        external_link: item.external_link
      }))

    }
  })
}

onMounted(() => {
  fetchNavCategory()
})

// 处理导航链接点击
const handleNavClick = (category) => {
  currentCategory.value = category.id
  if (category.external_link) {
    window.open(category.external_link, '_blank')
  }
}

const goToCart = () => {
  router.push('/cart')
}

const goToLearningCenter = () => {
  router.push('/user')
}

const goToLogin = () => {
  router.push('/login')
}

const goToRegister = () => {
  router.push('/register')
}

const goToHome = () => {
  router.push('/')
}
</script>

<style scoped>
header {
  background: #ffffff;
  border-bottom: 1px solid #e0e0e0;
  padding: 12px 0;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  display: flex;
  align-items: center;
}

.logo img {
  height: 40px;
  margin-right: 8px;
}

.logo span {
  font-size: 14px;
  color: #666666;
}

.nav-links {
  display: flex;
  align-items: center;
  gap: 30px;
}

.nav-links a {
  text-decoration: none;
  color: #333;
  font-size: 16px;
  position: relative;
  padding: 4px 0;
}

.nav-links a:hover,
.nav-links a.active {
  color: #ff6b00;
}

.nav-links a::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0;
  height: 2px;
  background: #ff6b00;
  transition: all 0.3s;
  transform: translateX(-50%);
}

.nav-links a:hover::after,
.nav-links a.active::after {
  width: 100%;
}

.search-container {
  display: flex;
  align-items: center;
  background: #f5f5f5;
  border-radius: 24px;
  overflow: hidden;
}

.search-container input {
  border: none;
  padding: 10px 18px;
  outline: none;
  width: 300px;
  font-size: 14px;
  background: transparent;
}

.search-container button {
  border: none;
  background: transparent;
  padding: 10px;
  cursor: pointer;
}

.user-action {
  display: flex;
  align-items: center;
  gap: 24px;
}

.action-links {
  display: flex;
  align-items: center;
  gap: 16px;
}

.icon-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  background: transparent;
  border: none;
  color: #666;
  font-size: 14px;
  cursor: pointer;
  padding: 4px 8px;
  transition: all 0.3s;
}

.icon-btn:hover {
  color: #ff6b00;
}

.icon-btn i {
  font-size: 16px;
}

.auth-buttons {
  display: flex;
  align-items: center;
  gap: 12px;
}

.login-btn, .register-btn {
  padding: 6px 16px;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
  border: none;
}

.login-btn {
  background: transparent;
  color: #666;
}

.login-btn:hover {
  color: #ff6b00;
}

.register-btn {
  background: #ff6b00;
  color: white;
}

.register-btn:hover {
  background: #ff8533;
}
</style>
